<template>
  <div class="app-container">
    <div v-show="!modifyInfo.visiable">
      <PageFilter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      >
        <template v-slot:filter-delete>
          <el-button :disabled="!canDelete" @click="del()">删除</el-button>
        </template>
      </PageFilter>
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="getPageRepairUser"
        :tab-index="false"
        :check-box="true"
        v-model:query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        @handleEvent="handleEvent"
      ></TableContainer>
    </div>
    <Modify
      v-if="modifyInfo.visiable"
      v-model:visible="modifyInfo.visiable"
      v-model:title="modifyInfo.title"
      :id="modifyInfo.id"
      @returnModify="search"
    />
    <SelectNoAuthUsers
      v-if="selectUserInfo.visible"
      v-model:showDialog="selectUserInfo.visible"
      v-model:query="selectUserInfo.query"
      :api="getPendRepairUserList"
      :saveEmpty="false"
      :check-box="true"
      @callback="selectUserCallBack"
    />
  </div>
</template>

<script setup name="repairUser">
import {
  getPageRepairUser,
  deleteRepairUser,
  getPendRepairUserList,
  batchInsertRepairUser,
} from '@/api/repair'
import Modify from './modify'
import SelectNoAuthUsers from '@/components/SelectUsers/selectNoAuthUsers'
const { proxy } = getCurrentInstance()
const selectUserInfo = ref({
  visible: false,
  saveEmpty: true,
  query: {
    type: 'normal',
  },
})
const filterInfo = ref({
  query: {
    keyword: '',
  },
  list: [
    {
      type: 'button',
      label: '新增',
      show: proxy.$permission('repairUser:add'),
      event: create,
    },
    {
      type: 'button',
      label: '批量新增',
      show: proxy.$permission('repairUser:batchAdd'),
      event: batchAdd,
    },
    {
      type: 'slot',
      label: '删除',
      value: 'delete',
      show: proxy.$permission('repairUser:delete'),
    },
    {
      type: 'input',
      label: '名称',
      value: 'nameLike',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  selections: [],
  fieldList: [
    { label: '姓名', value: 'name' },
    { label: '账号', value: 'userName' },
    { label: '联系电话', value: 'tel' },
    { label: '状态', value: 'statusValue' },
    { label: '部门', value: 'deptName' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '90',
    btList: [
      {
        label: '修改',
        event: update,
        show: proxy.$permission('repairUser:update'),
      },
      {
        label: '删除',
        event: del,
        show: proxy.$permission('repairUser:delete'),
      },
    ],
  },
})
const modifyInfo = ref({
  visiable: false,
  title: '',
  id: '',
})
const canDelete = computed(() => {
  return tableInfo.value.selections.length
})

function batchAdd() {
  //批量新增
  selectUserInfo.value.visible = true
}
function selectUserCallBack(data) {
  if (data.length) {
    let arr = []
    data.map((item) => {
      let obj = {
        userId: item.id,
        userName: item.name,
        ...item,
      }
      arr.push(obj)
    })
    batchInsertRepairUser(arr).then((resp) => {
      if (resp && resp.success) {
        getList()
      }
    })
  }
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}

// 搜索
function search() {
  // 重置分页
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
//新增
function create() {
  modifyInfo.value.visiable = true
  modifyInfo.value.title = '新增维修人员'
}
// 编辑
function update(row) {
  modifyInfo.value.visiable = true
  modifyInfo.value.id = row.id
  modifyInfo.value.title = '修改维修人员'
}
//删除
function del(row = undefined) {
  const data = []

  if (row) {
    data.push(row.id)
  } else {
    tableInfo.value.selections.forEach((item) => {
      data.push(item.id)
    })
  }
  deleteRepairUser(data).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = data
      break
  }
}
onMounted(() => {
  getList()
})
</script>

<style scoped></style>
